package com.qq.mobilemanager.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qq.mobilesafe.R;
import com.qq.mobilesafe.base.adapter.recyclerview.CommonAdapter;
import com.qq.mobilesafe.domain.TrafficInfo;
import com.qq.mobilesafe.recyclerview.adapter.SlideInBottomAnimatorAdapter;
import com.qq.mobilesafe.view.LoadingProgressDialog;

public class TrafficStatsActivity extends Activity {

	private List<TrafficInfo> list;

	@ViewInject(R.id.loading)
	private LinearLayout loading;

	private PackageManager mPm;
	private ScanTask task;

	@ViewInject(R.id.recyclerview)
	private RecyclerView recyclerView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initView();
		initData();
	}

	private void initData() {
		mPm = getPackageManager();
		task = new ScanTask();
		task.execute();
	}

	private void initView() {
		setContentView(R.layout.activity_traffic);
		ViewUtils.inject(this);
	}

	private class ScanTask extends AsyncTask<Void, Void, Void> {
		// private boolean isFinish = false;
		// private ListAdapter adapter;
		private LoadingProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new LoadingProgressDialog(TrafficStatsActivity.this,
					"拼命加载中...", R.drawable.loading_animation2);
			dialog.show();

		}

		@Override
		protected Void doInBackground(Void... params) {

			list = new ArrayList<TrafficInfo>();
			List<PackageInfo> installedPackages = mPm.getInstalledPackages(0);

			for (PackageInfo packageInfo : installedPackages) {
				TrafficInfo info = new TrafficInfo();

				String appname = packageInfo.applicationInfo.loadLabel(mPm)
						.toString();
				Drawable icon = packageInfo.applicationInfo.loadIcon(mPm);
				int uid = packageInfo.applicationInfo.uid;

				long snd = TrafficStats.getUidRxBytes(uid);
				long rcv = TrafficStats.getUidTxBytes(uid);

				if (snd != 0 && rcv != 0) {
					info.icon = icon;
					info.appname = appname;
					info.rcv = rcv;
					info.snd = snd;

					list.add(info);
				}

			}
			Collections.sort(list);
			return null;
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			super.onProgressUpdate(values);
			// if (adapter == null) {
			// adapter = new ListAdapter(TrafficStatsActivity.this, list);
			// recyclerView.setAdapter(adapter);
			// }else {
			// adapter.notifyDataSetChanged();
			// }

		}

		protected void onPostExecute(Void result) {
			dialog.dismiss();
			// loading.setVisibility(View.INVISIBLE);
			recyclerView.setVisibility(View.VISIBLE);
			// list_view.setVisibility(View.VISIBLE);
			// TrafficManagerAdapter adapter = new TrafficManagerAdapter();
			// list_view.setAdapter(adapter);
			setAdapter();

		};

		// private void stop() {
		// isFinish = true;
		// }

		// private void updateProgress() {
		//
		// }

	}
	public void setAdapter() {
		LinearLayoutManager layoutManager = new LinearLayoutManager(
				TrafficStatsActivity.this);
		recyclerView.setLayoutManager(layoutManager);
		ListAdapter adapter = new ListAdapter(TrafficStatsActivity.this,
				R.layout.item_traffic, list);
		SlideInBottomAnimatorAdapter slideInBottomAnimatorAdapter = new SlideInBottomAnimatorAdapter<>(
				adapter, recyclerView);
		recyclerView.setAdapter(slideInBottomAnimatorAdapter);
	}

	private class ListAdapter extends CommonAdapter<TrafficInfo> {
		private Context mContext;

		public ListAdapter(Context context, int layoutId,
				List<TrafficInfo> datas) {
			super(context, layoutId, datas);
			mContext = context;
		}

		@Override
		public void convert(com.qq.mobilesafe.base.adapter.ViewHolder holder,
				TrafficInfo trafficInfo) {
			holder.setText(R.id.tv_load,
					Formatter.formatFileSize(mContext, trafficInfo.snd));
			holder.setText(R.id.tv_up,
					Formatter.formatFileSize(mContext, trafficInfo.rcv));
			holder.setText(
					R.id.tv_sum,
					Formatter.formatFileSize(mContext, trafficInfo.snd
							+ trafficInfo.rcv));
			holder.setText(R.id.tv_appname, trafficInfo.appname);
			holder.setImageDrawable(R.id.iv_icon, trafficInfo.icon);
		}

	}

}
